<?php
/**
 * Router for Multi_Lang Module
 *
 * @package    Multi_Lang Module
 * @author     Kiall Mac Innes
 * @copyright  (c) 2007-2008 Multi_Lang Module Team..
 * @license    http://www.gnu.org/licenses/gpl-2.0.html
 */
class Router extends Router_Core
{
    public static $language = '';

    public static function find_uri()
    {
        Router_Core::find_uri();

        // TODO: Swap the !in_array() for a routes style regex... see Feature #2
        if (Config::item('multi_lang.enabled') and !in_array(Router::$current_uri, Config::item('multi_lang.ignore_uri')))
        {
            $allowed_languages = Config::item('multi_lang.allowed_languages');
            if (preg_match('~^[a-z]{2}(?=/|$)~i', Router::$current_uri, $matches)AND isset ($matches[0]))
            {
                // LC the language used in the url.
                $lang_lc = strtolower($matches[0]);

                // Check for invalid language in URL
                if (!array_key_exists($lang_lc, $allowed_languages))
                Event::run('system.404');

                // Set the currently defined language
                Router::$language = $lang_lc;

                // Remove the language from the URI
                Router::$current_uri = substr(Router::$current_uri, 3);

                if (Router::$current_uri == '')
                {
                    // Make sure the default route is set
                    $routes = Config::item('routes');
                    if (! isset ($routes['_default']))
                    throw new Kohana_Exception('core.no_default_route');

                    // Use the default route when no segments exist
                    Router::$current_uri = $routes['_default'];
                }
				
    			Config::set('locale.lang', Router::$language);
                Config::set('locale.language', $allowed_languages[Router::$language]);

                // GNU GetText Stuff
                if (function_exists('_'))
                {
                    setlocale(LC_ALL, $allowed_languages[Router::$language]);
                    putenv('LC_ALL='.$allowed_languages[Router::$language]);
                    bindtextdomain("application", DOCROOT."/application/i18n");
                    bindtextdomain("system", DOCROOT."/system/i18n");
                    textdomain("application");
                }
                // Overwrite setlocale which has already been set before in Kohana::setup(), and a few lines up.
                setlocale(LC_ALL, $allowed_languages[Router::$language].'.UTF-8');

                // Finally set a language cookie for 60 days
                cookie::set('lang', Router::$language, 5184000);
            }
            else
            {
                // Pick a language for the user and redirect

                // 1. Check for a language cookie.
                $new_langs[] = (string)cookie::get('lang');

                // 2. Look at HTTP_ACCEPT_LANGUAGE header
                /*foreach (Kohana::user_agent('languages') as $language)
                {
                    $new_langs[] = substr($language, 0, 2);
                }*/

                // 3. Final hard-coded fallback from config file
                $new_langs[] = Config::item('multi_lang.fallback_language');

                // Now loop through the new languages and stop at the first valid one
                foreach ($new_langs as $new_lang)
                {
                    if (array_key_exists($new_lang, $allowed_languages))
                    break;
                }

                // Redirect the user so the language appears in the browser url
                Router::$language = strtolower($new_lang); // Needed to allow url::site to give a correct url...
                url::redirect(url::site(Router::$current_uri));
            }
        }
    }

}
